package com.tools.api;

import com.tools.commons.constants.SystemConstants;
import com.tools.commons.exceptions.BadRequestException;
import com.tools.commons.utils.BeanUtils;
import com.tools.commons.utils.ValidationUtils;
import com.tools.model.entity.InfoEntity;
import com.tools.model.entity.TasksEntity;
import com.tools.model.entity.UserEntity;
import com.tools.model.entity.UserMsgRelEntity;
import com.tools.model.entity.UserTasksEntity;
import com.tools.model.params.*;
import com.tools.security.context.SecurityContext;
import com.tools.security.context.SecurityContextHolder;
import com.tools.security.context.UserInfo;
import com.tools.security.utils.IsDoctor;
import com.tools.service.InfoService;
import com.tools.service.PicService;
import com.tools.service.TaskService;
import com.tools.service.UserMsgsService;
import com.tools.service.UserService;
import com.tools.service.UserTaskSerive;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangminggen
 */
@RestController
@RequestMapping(SystemConstants.Api.USERS)
@RequiredArgsConstructor
public class InfoController {
    private final InfoService infoService;

    @ApiOperation("初始化信息")
    @PostMapping("initInfo")
    public void initDoctorInfo(@RequestBody InfoParams infoParams) {
        @NonNull SecurityContext context = SecurityContextHolder.getContext();
        if(context.getUserInfo().isDoctor()){
            if(infoParams.getHospital()==null&&infoParams.getSection()==null){
                throw new BadRequestException("医院或者科室不可为空");
            }
        }
        if(!context.getUserInfo().isDoctor()){
            if(infoParams.getAges()==null){
                throw new BadRequestException("年龄不能为空");
            }
        }
        InfoEntity infoEntity = new InfoEntity();
        BeanUtils.updateProperties(infoParams, infoEntity);
        infoEntity.setDoctor(context.getUserInfo().isDoctor());
        infoEntity.setUserId(context.getUserInfo().getUserId());
        infoService.save(infoEntity);
    }

    @ApiOperation("获取详细信息")
    @GetMapping("userInfo")
    public InfoEntity userInfo() {
        SecurityContext context = SecurityContextHolder.getContext();
        UserInfo userInfo = context.getUserInfo();
        return infoService.findByUserId(userInfo.getUserId());
    }
}
